<link rel="stylesheet" type="text/css" href="/assets/css/ward.css" />
<div class="red" id="red"><img src="/assets/img/gift02.png" srcs="0" class="imgbox"></div>
<input type="hidden" name="matitle" value="{$typeReuslt.typename}" />
<input type="hidden" name="urladdres" value="{$getURL}" />
<input type="hidden" name="randcode" value="{$randcode}" />
<input type="hidden" name="appid" value="{$portinfo['u_appid']}" />
<input type="hidden" name="openid" value="{$Think.cookie.user_userinfo}" />
<input type="hidden" name="getshaddres" value="{$reginfo|count}" />
<input type="hidden" name="userinfoid" value="{$reginfo['id']}">
<style type="text/css">
.saddress{text-align: left; line-height: 24px;}
.saddress input{display: inline; clear: none; float: none; height: auto;}
</style>
<switch name="award.awardkind">
	<case value="2">
		<!--实物奖品-->
		<div class="poplayerma">
			<div class="float-gift">
				<div class="getward">
					<div class="w_left">恭喜您，获得“{$award['awardname']}”<b>注：请填写以下相关信息，以保证奖品及时送达您手中。</b></div>
					<div class="w_right"><img class="jpimg" src="{$award['awardimg']}" height="60"/><span>奖品缩略图</span></div>
				</div>
				<if condition="$reginfo|count neq 0">
				<div class="getshaddres">
					<div class="input-box clearfix">
						<label for="username">收货地址:</label>
					</div>
					<div class="input-box clearfix saddress">
						<input type="radio" name="saddress" value="1" checked> {$reginfo['username']} {$reginfo['address']}
					</div>
				</div>
				<elseif condition="$reginfo|count eq 0" />
					<div class="otherinfo">
						<div class="input-box clearfix">
							<label for="username">姓名:</label>
							<input type="text" name="username" id="username">
						</div>
						<div class="input-box clearfix">
							<label for="usertel">电话:</label>
							<input name="usertel" type="tel" id="usertel">
						</div>
						<div class="input-box clearfix">
							<label for="jishi"></label>
							<input name="jishi" type="button" id="jishi" value="获取验证码">
						</div>
						<div class="input-box clearfix">
							<label for="userpass">验证码:</label>
							<input type="text" name="userpass" id="userpass">
						</div>
						<div class="input-box clearfix">
							<label for="address">地址:</label>
							<input type="text" name="address" id="address">
						</div>
					</div>
				</if>
				<p>请提交信息，我们会尽快将奖品送出</p>
				<if condition="$rootid eq 275">
				<p style="font-size: 16px;">兑奖热线：<a href="tel:4006006955" style="display: inline; color: #ff5c5d">4006006955</a></p>
				</if>
				<div class="btn-box clearfix">
					<a class="sure-btn">确认领取</a>
					<a class="del-btn">取消</a>
				</div>
			</div>
		</div>
	</case>
	<case value="3">
	<!--红包-->
		<div class="poplayerma" style="margin-top: 20%;">
			<div class="float-gift" style="padding:0px; background-color: #fff2c5;">
				<div class="float-bg"><img src="/assets/img/float-bg.png"></div>
				<div class="content-box" style="width: 100%; padding-top: 40px;">
					<h4 id="h4html">-</h4>
					<div class="gift-box">
						<div class="pic-gift"><img src="/assets/img/redbag.png"></div>
					</div>
					<span>请返回微信最近联系人列表栏<br/>置顶公众号或服务通知中领取红包</span>
				</div>
			</div>
		</div>

	</case>
	<default />
	<!--华人钻-->
	<div class="poplayerma" style="margin-top: 20%;">
		<div class="float-gift" style="padding:0px; background-color: #fff2c5;">
			<div class="float-bg"><img src="/assets/img/float-bg.png"></div>
			<div class="content-box" style="width: 100%; padding-top: 40px;">
				<h4 id="h4html">-</h4>
				<div class="gift-box">
					<div class="pic-gift"><img src="/assets/img/diamond.png"></div>
				</div>
				<a href="http://hrshop.hrtv.cn/index.php?controller=ucenter&action=index" class="alink-btn"><img src="/assets/img/btn.png"><i>点击查看</i></a>
			</div>

		</div>
	</div>
</switch>

<script type="text/javascript">
$(function(){
	var openid = $("input[name='openid']").val();
	var randcode = $("input[name='randcode']").val();
	var matitle = $("input[name='matitle']").val();
	$("input[name='saddress']").change(function(){
		$(".otherinfo").hide();
	})
	if($("input[name='getshaddres']").val()==0){
		$(".getshaddres").hide();
	}else{
		$(".otherinfo").show();
	}
	//判断此码的奖品是否被领取
	function checkwardget(){
		var red = $(".red");
		var send_url = "/Home/Batch/is_checkawardget";
		var senddata = {"randcode":randcode,random:Math.random()};
		$.ajax({
			url:send_url,
			data:senddata,
			type:"POST",
			dataType:"html",
			success:function (data){
				if(data==1){
					red.removeClass("shake");
					red.find(".imgbox").attr("srcs",1);
					red.find(".imgbox").attr("src","/assets/img/gift01.png");
					alert("此奖品已被人领取！");
					$(".poplayerma").hide();
					$(".red").hide();
				}else{

					var matype = {$award['id']};
					//先查询此奖品是什么类型的
					var savetel_url = "/Home/Batch/chaxun_award";
					var teldata = {"matype":matype,random:Math.random()};
					$.ajax({
						url:savetel_url,
						data:teldata,
						type:"POST",
						dataType:"JSON",
						success:function (data){
							var v = $.trim(data.awardkind);
							if(v==2){
								chstatus();
							}else if(v==1){
								getdrill("来自"+matitle+"的抽奖获得");
							}else if(v==3){
								gethb();
							}
							return false;
						}
					});
					return false;

				}
			}
		});
	}
	function chstatus(){
		$(".poplayerma").fadeIn();
		var red = $(".red");
		red.removeClass("shake");
		//$(".opacity").fadeIn();
		red.find(".imgbox").attr("srcs",1);
		red.find(".imgbox").attr("src","/assets/img/gift01.png");
	}
	//恭喜您获得红包,发红包
	function gethb(){
		var appid = $("input[name='appid']").val();
		insertdata(openid,appid,randcode);

	}
	//插入数据库
	function insertdata(getopenid,appid,randcode){
		var rootid = '{$rootid}';
		var getdata_url = "/Home/Index/sendhb";
		var data = {"randcode":randcode,"rootid":rootid,"appid":appid,random:Math.random()};
		$.ajax({
			url:getdata_url,
			data:data,
			type:"post",
			dataType:"html",
			success:function (val){
					if(val!=-1 && val!=-2 && val!=-3){
						//红包发送成功后
						chstatus();
						$("#h4html").html("恭喜您，获得 "+val+" 元红包！");
						changecishu(3,randcode,getopenid);
					}else if(val==-1 || val!=-2 || val!=-3){
						chstatus();
						$("#h4html").html("红包已抢完！");
						return false;
					}
			},
			error:function(val){
				alert('请求失败！');
			}
		});


		//if(getopenid!=''){
//			var getdata_url = "http://pay.hrtv.cn/hongbao/sendredpack";
//			var data = {"re_openid":getopenid,"act_name":act_name,"send_name":send_name,"remark":remark,"wishing":wishing,"confirm":confirms,"appid":appid};
//			$.ajax({
//				url:getdata_url,
//				data:data,
//				type:"GET",
//				dataType:"JSON",
//				success:function (val){
//					if(val['statuss']==1){
//						//红包发送成功后
//						var total_amount = val['total_amount']; //金额
//						chstatus();
//						$("#h4html").html("恭喜您，获得 <em style='font-style:normal;color:#ec0f0f;'>"+total_amount+"</em> 元红包！");
//						changecishu(3,randcode,getopenid);
//					}else{
//						if(val['info']=="系统错误:帐号余额不足，请到商户平台充值后再重试"){
//							chstatus();
//							$("#h4html").html("红包已抢完！");
//							return false;
//						}else if(val['info']=="系统错误:发放失败，此请求可能存在风险，已被微信拦截"){
//							chstatus();
//							$("#h4html").html("红包已抢完！");
//							return false;
//						}
//					}
//				},
//				error:function(val){
//					alert('请求失败！');
//				}
//			});
		//}else{
			//location.href="http://e.hrtv.cn/ehrtv/Login/index?url={$getURL}";
		//}
	}

	//恭喜您获得华人钻石,添加到账户
	function getdrill(bodystr){
		var diamond = getrandom(5,10);
		var savetel_url = "/Home/Batch/adddrill";
		var teldata = {"diamond":diamond,"bodystr":bodystr,random:Math.random()};
		$.ajax({
			url:savetel_url,
			data:teldata,
			type:"POST",
			dataType:"html",
			success:function (data){
				if(data==1){
					chstatus();
					$("#h4html").html("恭喜您，获得 <em style='font-style:normal;color:#ec0f0f;'>"+diamond+"</em> 个华人钻");
					var userinfoid = $("input[name='userinfoid']").val();
					changecishu(1,randcode,userinfoid);
					return false;
				}
			}
		});
	}
	//随机产生华人钻数量
	function getrandom(min,max){
		return Math.floor(min+Math.random()*(max-min));
	}

	$("#red").click(function(){
		var restrict = '{$restrict}';
		if(restrict!=1){
			$(this).removeAttr('id');
			setTimeout(function(){
				$(".red").addClass("shake");
				checkwardget();
			},1);
			$(this).find(".imgbox").attr("srcs",0);
			$(this).find(".imgbox").attr("src","/assets/img/gift02.png");
			return false;
		}else{
			alert('您今天已领取过2次奖品，请明天再来！');
			$(".poplayerma").hide();
			$(".red").hide();
		}

	});

	$(".del-btn").click(function(){
		$(".poplayerma").hide();
		$(".red").hide();
	});
	$(".poplayerma").click(function(event){
		$(".poplayerma").hide();
		$(".red").hide();
		event.stopPropagation();
	});
	$(".float-gift").click(function(event){
		event.stopPropagation();
	})
	//倒计时
	var countdown = 30;
	function settime() {
		if (countdown == 0) {
			$("#jishi").val("获取验证码");
			$("#jishi").removeAttr('disabled');
			countdown = 30;
			return;
		} else {
			$("#jishi").val("重新发送("+countdown+")");
			countdown--;
		}
		setTimeout(function() {
			settime()
		},1000)
	}
	/*发送手机验证码*/
	$("#jishi").click(function(){
		var mobile = $("input[name='usertel']").val();
		var reg = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
		if (!reg.test(mobile)) {
			alert("请输入正确的手机号码！");
			$("input[name='usertel']").val(null);
			$("input[name='usertel']").focus();
			return false;
		}else{
			//先判断是否有注册信息
			var savetel_url = "/Home/Batch/isreginfo";
			var teldata = {"usertel":mobile,random:Math.random()};
			$.ajax({
				url:savetel_url,
				data:teldata,
				type:"POST",
				dataType:"html",
				success:function (data){
					if(data==0){
						var jis = $("input[name='jishi']");
						jis.attr('disabled','disabled');
						var send_url = "/Home/Batch/verifynote";
						var senddata = {"usertel":mobile,random:Math.random()};
						$.ajax({
							url:send_url,
							data:senddata,
							type:"POST",
							dataType:"html",
							success:function (data){
							}
						});
						$("input[name='userpass']").focus();
						settime();
					}
				}
			});
		}
	})

	/*保存注册信息*/
	$("a.sure-btn").click(function(){
		var rootid = '{$rootid}';
		var userpass = $("input[name='userpass']").val();
		var mobile = $("input[name='usertel']").val();
		var username = $("input[name='username']").val();
		var address = $("input[name='address']").val();
		var saddress = $("input[name='saddress']").val();
		if(saddress==1){
			$(this).removeClass('sure-btn');
			var userinfoid = $("input[name='userinfoid']").val();
			/*改变此码的领取记录*/
			addreginfos(rootid,"{$reginfo['tel']}",openid,randcode,"{$reginfo['username']}","{$reginfo['address']}");
			//changecishu(2,randcode,userinfoid);
		}else{
			if($.trim(username)==''){
				alert('请填写姓名！');
				return false;
			}
			if($.trim(address)==''){
				alert('请填写收货地址！');
				return false;
			}
			$(this).removeClass('sure-btn');
			var vget_url = "/Home/Batch/getverifynote";
			var vdata = {"userpass":userpass,random:Math.random()};
			$.ajax({
				url:vget_url,
				data:vdata,
				type:"POST",
				dataType:"html",
				success:function (data){
					/*判断验证码是否正确*/
					if(data==1){
						/*手机验证通过*/
						addreginfos(rootid,mobile,openid,randcode,username,address);
					}else{
						alert("验证不正确，请重新获取！");
						return false;
					}

				}
			});
		}
	})

	//保存信息
	function addreginfos(rootid,mobile,openid,randcode,username,address){
		var send_url = "/Home/Batch/addresreg";
		var senddata = {"rootid":rootid,"usertel":mobile,"openid":openid,"randcode":randcode,"username":username,"address":address,random:Math.random()};
		$.ajax({
			url:send_url,
			data:senddata,
			type:"POST",
			dataType:"html",
			success:function (data){
				if(data>0){
					/*改变此码的领取记录*/
					changecishu(2,randcode,data);
					$(".otherinfo input").each(function(index,element){
						$(this).val(null);
					})
				}else{
					alert("提交失败，请重新填写！");
					$(".otherinfo input").each(function(index,element){
						$(this).val(null);
					})
				}
			}
		});
	}

	function changecishu(matype,randcode,userinfoid){
		var send_url = "/Home/Batch/changema_cishu";
		var senddata = {"randcode":randcode,"userinfoid":userinfoid,random:Math.random()};
		$.ajax({
			url:send_url,
			data:senddata,
			type:"POST",
			dataType:"html",
			success:function (data){
				if(data==1){
					if(matype==2){
						alert("信息提交成功，我们会尽快将奖品送出！");
						$(".poplayerma").hide();
						$(".red").hide();
					}else if(matype==3 || matype==1){
						//如果是红包码或者钻石码，则插入用户的openid
						insertuseropenid(randcode);
					}
				}else if(data==0){
					if(matype==2){
						alert("您已领取过此码的奖品");
						$(".poplayerma").hide();
						$(".red").hide();
					}
				}else{
					alert("error");
				}
			}
		});
	}

	function insertuseropenid(randcode){
		var send_url = "/Home/Batch/saveuseropenid";
		var senddata = {"randcode":randcode,random:Math.random()};
		$.ajax({
			url:send_url,
			data:senddata,
			type:"POST",
			dataType:"JSON",
			success:function (data){
			}
		});
	}

	function writeObj(obj){
		var description = "";
		for(var i in obj){
			var property=obj[i];
			description+=i+" = "+property+"\n";
		}
		alert(description);
	}

});
</script>
